<?php
/*********
*    首页控制器
*/
namespace Home\Controller;
use Home\Controller\HomeController;
class CartController extends HomeController {
 	public function _initialize(){
 		parent::_initialize();
 		
 	}
	/**
	 * 添加购物车
	 * 
	 */
 	public function addCart(){
 		$open_id = I('openid');
 		$goods_id = I('goods_id');
 		$price_attr_id = I('price_attr_id');
 		$other_attr = I('other_attr');
 		//$other_attr = implode(',',$other_attr);
 		$num = I('num');
 		/* dump($goods_id);
 		dump($price_attr_id);
 		dump($other_attr);
 		dump($num);die; */
 		$cart = [
			'open_id'=>$open_id,
			'goods_id'=>$goods_id,
			'goods_attr_id'=>$price_attr_id,
			'attr_names'=>$other_attr,
 		];
 		$cart_find = M('Cart')->where($cart)->find();
 		
 		if($cart_find){
 			$r = M('Cart')->where($cart)->setInc('num',$num);   
 		}else{
 		    $cart['num']=$num;
 			$r = M('Cart')->add($cart);
 		}
		if(!$r){
			$this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙'],'JSONP');
		}
		$this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
 	}
 	/**
 	 * 获取购物车信息
 	 */
 	public function getCartByOpenId(){
 		$open_id = I('openid');
 		$cart = M('Cart')->where(array('open_id'=>$open_id))->save(array('is_check'=>0));
 		$cart = M('Cart')->where(array('open_id'=>$open_id))->select();
 		if($cart){
 			foreach ($cart as $k=>$v){
 				$cart['goods'] = M('Goods')->where(array('goods_id'=>$v['goods_id']))->find();
 				$price = M('Goods_attr')->where(array('goods_attr_id'=>$v['goods_attr_id']))->find();
 				$attr_price = $price['price'];
 				/* $cart['price'] = $attr_price;
 				$cart['num'] = intval($v['num']); */
 				$cartInfo[$k]['cart_id'] = $v['cart_id'];
 				$cartInfo[$k]['goodsName'] = $cart['goods']['name'];
 				$cartInfo[$k]['attr_fan'] = $price['fanjifen'];
 				$cartInfo[$k]['goodsImg'] = $cart['goods']['image'];
 				$cartInfo[$k]['num'] = intval($v['num']);
 				$cartInfo[$k]['is_check'] = $v['is_check'];
 				$cartInfo[$k]['price'] = $attr_price;
 				$cartInfo[$k]['attr_kucun'] = $price['attr_kucun'];
 				$cartInfo[$k]['shuxing'] = $v['attr_names'];
 				$cartInfo[$k]['jifen'] = $price['jifen'];
 				$cartInfo[$k]['goods_id'] = $price['goods_id'];
 				$cart = M('Cart')->where('cart_id ='.$v['cart_id'])->find();
 				$kucun = M('Goods_attr')->where('goods_attr_id ='.$cart['goods_attr_id'])->find();
 				if($kucun['attr_kucun'] < $cart['num']){
 				    $cartInfo[$k]['kucun_num'] = 1;
 				}
 			}
 		}
 		$this->ajaxReturn(['status'=>1,'msg'=>'请求成功','cartInfo'=>$cartInfo]);
 	} 
 	/**
 	 * 修改购物车数量
 	 */
 	public function changeCartNum(){
 		$num = I('num');
 		$open_id = session('openid');
 		$cart_id = I('cart_id');
 		//dump($num);die;
 		$cart = M('Cart')->where('cart_id ='.$cart_id)->find();
 		$kucun = M('Goods_attr')->where('goods_attr_id ='.$cart['goods_attr_id'])->find();
 		if($kucun['attr_kucun'] < $num){
 		    $this->ajaxReturn(['status'=>-3,'msg'=>'商品库存不足'],'JSONP');
 		}
 		$r = M('Cart')->where(array('cart_id'=>$cart_id))->setField('num',$num);
 		if(!$r){
 			$this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙'],'JSONP');
 		}else{
 		    $this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
 		}
 		
 	}
 	/**
 	 * 删除购物车
 	 */
 	public function delCart(){
 	    $open_id = session('openid');
 	    $cart_id = I('cart_id');
 	    $r = M('Cart')->where(array('cart_id'=>$cart_id,'open_id'=>$open_id))->delete();
 	    if(!$r){
 	     	$this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙'],'JSONP');
 	    }
 	      $this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
 	}
 	
 	/**
 	 * 选中购物车
 	*/
 	public function checkCart(){
 	    $open_id = session('openid');
 	    $cart_id = I('cart_id');
 	    $cart = M('Cart')->where('cart_id ='.$cart_id)->find();
            $goods_attr = M('Goods_attr')->where(array('goods_attr_id'=>$cart['goods_attr_id']))->find();
           // if(!empty($goods_attr['price'])){
             // 	$status = 1;
            //}
            $carts = M('Cart')->where(array('open_id'=>$open_id,'is_check'=>1,'cart_id'=>array('neq',$cart_id)))->select();
            if($carts){
            
                   foreach ($carts as $v){
                      $goods_attrs = M('Goods_attr')->where(array('goods_attr_id'=>$v['goods_attr_id']))->find();
                      if(!empty($goods_attr['price'])&&!empty($goods_attrs['jifen'])){
                          $this->ajaxReturn(['status'=>-2,'msg'=>'积分商品和现金商品不能同时购买'],'JSONP');
                      }
                       if(!empty($goods_attr['jifen'])&&!empty($goods_attrs['price'])){
                          $this->ajaxReturn(['status'=>-2,'msg'=>'积分商品和现金商品不能同时购买'],'JSONP');
                      }
                  }
            }
           
 	    $kucun = M('Goods_attr')->where('goods_attr_id ='.$cart['goods_attr_id'])->find();
 	    if($kucun['attr_kucun'] < $cart['num']){
 	        $this->ajaxReturn(['status'=>-2,'msg'=>'所选商品库存不足'],'JSONP');
 	    }
 	    $is_check = I('is_check');
 	    $r = M('Cart')->where(array('cart_id'=>$cart_id))->setField('is_check',$is_check);
 	    if(!$r){
 	        $this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙'],'JSONP');
 	    }else{
 	        $this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
 	    }
 	    
 	}
 	/**
 	 * 选中购物车(全选)
 	*/
 	public function checkCartAll(){
 	    $open_id = session('openid');
 	    $is_check = I('is_check');
 	    $cart = M('Cart')->where(array('open_id'=>$open_id))->select();
            $price = [];
            $jifen = [];
 	    foreach($cart as &$v){
 	        $kucun = M('Goods_attr')->where('goods_attr_id ='.$v['goods_attr_id'])->find();
 	        $cart_n = M('Cart')->where('cart_id ='.$v['cart_id'])->find();
 	        if($kucun['attr_kucun'] < $cart_n['num']){
 	            $kucun_num[] = 1;
 	        }
                if(!empty($kucun['jifen'])){
                  	$jifen[] = $kucun['jifen'];
                }
                if(!empty($kucun['price'])){
                  	$price[] = $kucun['price'];
                }
 	    }
            if(!empty($jifen)&&!empty($price)&&(count($jifen)+count('price')!=1)){
              	$this->ajaxReturn(['status'=>-2,'msg'=>'积分商品和现金商品不能同时购买'],'JSONP');
            }
 	    if($is_check == 1){
 	        if(in_array(1,$kucun_num)){
 	            $this->ajaxReturn(['status'=>-2,'msg'=>'购物车内商品库存不足'],'JSONP');
 	        }
 	    }
 	    
 	    $r = M('Cart')->where(array('open_id'=>$open_id))->setField('is_check',$is_check);
 	    if(!$r){
 	        $this->ajaxReturn(['status'=>-1,'msg'=>'你已经选中了哦'],'JSONP');
 	    }
 	    $this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
 	}
}
